import { createBrowserRouter, Navigate } from 'react-router-dom';
import Login from './pages/Login';
import StudentLayout from './pages/student/Layout';
import Home from './pages/student/Home';
import CoursePool from './pages/student/CoursePool';
import Selected from './pages/student/Selected';
import Profile from './pages/student/Profile';
import { RequireRole } from './auth/RequireRole';
import TeacherLayout from './pages/teacher/Layout';
import TeacherHome from './pages/teacher/Home';
import TeacherHomeJS from './pages/teacher/TeacherHome';
import TeacherCourses from './pages/teacher/Courses';
import TeacherStudents from './pages/teacher/Students';
import ApplyCourse from './pages/teacher/ApplyCourse';
import AppliedCourses from './pages/teacher/AppliedCourses.jsx';
import CourseStudents from './pages/teacher/CourseStudents';
import TeacherProfile from './pages/teacher/MyProfile';
import AdminLayout from './pages/admin/Layout';
import AdminDashboard from './pages/admin/Dashboard';
import AdminHome from './pages/admin/AdminHome.jsx';
import Approvals from './pages/admin/Approvals';
import Users from './pages/admin/Users';
import Review from './pages/admin/Review.jsx';
import SearchPage from './pages/admin/SearchPage';
import AddUser from './pages/admin/AddUser';
import AdminProfile from './pages/admin/AdminProfile';

const RequireAuth = ({ children }: { children: JSX.Element }) => {
  const token = localStorage.getItem('token');
  // 如果没有token，直接跳转到登录页
  if (!token) return <Navigate to="/login" replace />;
  return children;
};

export const router = createBrowserRouter([
  { path: '/login', element: <Login /> },
  {
    path: '/',
    element: (
      <RequireAuth>
        <StudentLayout />
      </RequireAuth>
    ),
    children: [
      { index: true, element: <Home /> },
      { path: 'courses', element: <CoursePool /> },
      { path: 'selected', element: <Selected /> },
      { path: 'me', element: <Profile /> },
    ],
  },
  {
    path: '/teacher',
    element: (
      <RequireRole role="teacher">
        <TeacherLayout />
      </RequireRole>
    ),
    children: [
      { index: true, element: <TeacherHomeJS /> },
      { path: 'home-old', element: <TeacherHome /> },
      { path: 'courses', element: <TeacherCourses /> },
      { path: 'students', element: <TeacherStudents /> },
      { path: 'apply', element: <ApplyCourse /> },
      { path: 'applied', element: <AppliedCourses /> },
      { path: 'course-students', element: <CourseStudents /> },
      { path: 'profile', element: <TeacherProfile /> },
    ],
  },
  {
    path: '/admin',
    element: (
      <RequireRole role="admin">
        <AdminLayout />
      </RequireRole>
    ),
    children: [
      { index: true, element: <AdminHome /> },
      { path: 'dashboard', element: <AdminDashboard /> },
      { path: 'approvals', element: <Approvals /> },
      { path: 'review', element: <Review /> },
      { path: 'users', element: <Users /> },
      { path: 'search', element: <SearchPage /> },
      { path: 'add-student', element: <AddUser /> },
      { path: 'profile', element: <AdminProfile /> },
    ],
  },
  { path: '*', element: <Navigate to="/" replace /> },
]);


